package com.wu.store.dao;

import com.wu.store.entity.Category;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CategoryDao {
	@Select("<script>" +
		        "select * from t_category " +
		        "where status = #{status} " +
		        "<if test='keyword != null'>" +
		        "and name like '%${keyword}%'" +
		        "</if>" +
		        "order by create_time desc" +
		        "</script>")
	List<Category> selectByKeywordAndStatus(String keyword, int status);

	@Select("select * from t_category where name = #{name}")
	Category selectByName(String name);

	@Select("select * from t_category where id = #{id}")
	Category selectById(Integer id);

	@Options(useGeneratedKeys = true, keyProperty = "category.id")
	@Insert(value = "insert into t_category (name, create_time, parent_id) values (#{category.name}, #{category.createTime}, #{category.parentId})")
	@SelectKey(before = false, keyProperty = "category.id", statement = "SELECT LAST_INSERT_ID()", resultType = Long.class)
	void add(@Param("category") Category category);

	@Update("<script>" +
		        "update t_category set name = #{name}, update_time = #{updateTime}, status = #{status}, parent_id = #{parentId} where id = #{id}" +
		        "</script>")
	void update(Category category);
}
